package com.ossjk.oa.attendance.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.attendance.entity.Attendance;
import com.ossjk.oa.attendance.service.IAttendanceService;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.service.IDepartmentService;
import com.ossjk.oa.system.service.IEmployeeService;

/**
 * <p>
 * 考勤 前端控制器
 * </p>
 *
 * @author chair
 * @since 2019-02-03
 */
@RestController
@RequestMapping("/attendance/attendance")
public class AttendanceController extends BaseController {
	@Autowired
	private IAttendanceService iAttendanceService;
	@Autowired
	private IEmployeeService iEmployeeService;
	@Autowired
	private IDepartmentService iDepartmentService;

	@RequiresPermissions("00050000")
	@GetMapping("/list")
	public Object list(String did, String eid, String bgdate, String eddate, String stat, String rwst, Page page) {
		Wrapper wrapper = Condition.create().isWhere(true).eq("ad.isdelete", "1").orderBy("date", false);
		if (!CommonUtil.isBlank(did)) {
			wrapper.eq("ad.did", did);
		}
		if (!CommonUtil.isBlank(eid)) {
			wrapper.eq("ad.eid", eid);
		} else {
			// 默认查询自己
			wrapper.eq("ad.eid", getCurrnetUserId());
		}
		if (!CommonUtil.isBlank(bgdate)) {
			wrapper.ge("ad.date", bgdate);
		}
		if (!CommonUtil.isBlank(eddate)) {
			wrapper.le("ad.date", eddate);
		}
		if (!CommonUtil.isBlank(stat)) {
			wrapper.eq("ad.stat", stat);
		}
		if (!CommonUtil.isBlank(rwst)) {
			wrapper.eq("ad.rwst", rwst);
		}
		page = iAttendanceService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("000500000000")
	@GetMapping("/toInsert")
	public Object toInsert() {
		// 返回当前用户
		return responseSuccess(iEmployeeService.selectById(getCurrnetUserId()));
	}

	@RequiresPermissions("000500000000")
	@PostMapping("/insert")
	public Object insert(Attendance attendance) {
		attendance.setEid(getCurrnetUserId());
		// 判断是否已有考勤记录
		// 格式化Date 如: yyyyMMdd
		String dateStr = CommonUtil.date0(attendance.getDate());
		// 年
		attendance.setYear(new BigDecimal(dateStr.substring(0, 4)));
		// 月
		attendance.setMonth(new BigDecimal(dateStr.substring(4, 6)));
		// 日
		attendance.setDay(new BigDecimal(dateStr.substring(6, 8)));
		Wrapper wrapper = Condition.create().eq("eid", attendance.getEid()).eq("year", attendance.getYear()).eq("month", attendance.getMonth()).eq("day", attendance.getDay());
		if (!CommonUtil.isBlank(iAttendanceService.selectOne(wrapper))) {
			return responseFail("已存在考勤记录");
		}
		// 查询该员工的部门
		Department department = iDepartmentService.selectByEid(attendance.getEid());
		// 设置部门id
		attendance.setDid(department.getId());
		// 异常
		attendance.setStat(new BigDecimal(2));
		// 待审核
		attendance.setRwst(new BigDecimal(1));
		if (iAttendanceService.insert(attendance)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000500000005")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		return responseSuccess(iAttendanceService.selectById(id));
	}

	@RequiresPermissions("000500000005")
	@PutMapping("/update")
	public Object update(Attendance attendance) {
		if (!CommonUtil.isEquals(attendance.getRwst(), new BigDecimal(1))) {
			attendance.setRwtm(new Date());
			attendance.setRwid(getCurrnetUserId());
		}
		if (iAttendanceService.updateById(attendance)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("000500000035")
	@GetMapping("/toUpdateDetail/{id}")
	public Object toUpdateDetail(@PathVariable("id") String id) {
		Map map = new HashMap<>();
		Attendance attendance = iAttendanceService.selectById(id);
		map.put("attendance", attendance);
		Employee employee = iEmployeeService.selectById(attendance.getEid());
		map.put("employee", employee);
		Department department = iDepartmentService.selectById(attendance.getDid());
		map.put("department", department);
		Employee rw = iEmployeeService.selectById(attendance.getRwid());
		map.put("rw", rw);
		return responseSuccess(map);
	}

	@RequiresPermissions("000500000010")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iAttendanceService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000500000015")
	@PostMapping("/importExcel")
	public Object importExcel(MultipartFile file) throws Exception {
		// System.out.println(this.getClass().getResource("").toString());
		// System.out.println(this.getClass().getResource("/").toString());
		File destDirectory = new File(this.getClass().getResource("/").getFile() + File.separator + "tmp");
		if (!destDirectory.exists()) {
			destDirectory.mkdirs();
		}
		File destFile = new File(this.getClass().getResource("/").getFile() + File.separator + "tmp" + File.separator + file.getOriginalFilename());
		file.transferTo(destFile);
		if (iAttendanceService.batchImportExcel(destFile)) {
			destFile.delete();
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
